import { post, get } from "../../../utils/request";
import utils from "../../../utils/utils";

const env = import.meta.env;
const contextPath = env.VITE_API_AUTH_CONTEXT_PATH;
const url = env.VITE_API_URL;

const apiAuth = {
  generateSessionPublicKeyQ(sessionId: string) {
    return post(contextPath + "/auth/v1/public.key.get", {
      sessionId,
    });
  },
  generateCaptchaUrl(sessionId: string) {
    // 其他方法是 post，在 request.ts 中指定了 env.VITE_API_URL，当前方法仅返回一个 url，所以需要单独配置
    return (
      (url ? url : "") +
      contextPath +
      "/auth/v1/login.captcha?sessionId=" +
      sessionId +
      "&t=" +
      utils.getUUID()
    );
  },
  loginByAccount(data?: object) {
    return post(contextPath + "/auth/v1/login.by.account", data);
  },
  sendEmailCaptchaByForgotPassword(sessionId: string, email: string) {
    return post(
      contextPath + "/auth/v1/send.email.captcha.by.forgot.password",
      {
        sessionId,
        email,
      }
    );
  },
  validateEmailByForgotPassword(sessionId: string, captcha: string) {
    return post(contextPath + "/auth/v1/validate.email.by.forgot.password", {
      sessionId,
      captcha,
    });
  },
  resetPassword(sessionId: string, password: string) {
    return post(contextPath + "/auth/v1/reset.password", {
      sessionId,
      password,
    });
  },
  sendEmailCaptchaBySignup(sessionId: string, email: string) {
    return post(contextPath + "/auth/v1/send.email.captcha.by.signup", {
      sessionId,
      email,
    });
  },
  validateEmailBySignup(sessionId: string, captcha: string) {
    return post(contextPath + "/auth/v1/validate.email.by.signup", {
      sessionId,
      captcha,
    });
  },
  signup(sessionId: string, accountSignup: string) {
    return post(contextPath + "/auth/v1/signup", {
      sessionId,
      accountSignup,
    });
  },
};

export default apiAuth;
